<template>
  <el-container>
    <el-header>
      <div class="logo_bt">
        <img src="../assets/yang.png" alt />
        <span>yang电商管理系统</span>
      </div>
      <el-button type="info" @click="Logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width=" isCollapse ?'64px':'200px'">
        <div class="toggle-button" @click="toggle">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级选项 -->
            <el-menu-item
              :index="'/'+s_item.path"
              v-for="s_item in item.children"
              :key="s_item.id"
              @click="saveState('/' + s_item.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{s_item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      isCollapse: false,
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-3702mima',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    Logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    toggle() {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList() {
      const { data: result } = await this.$http.get('menus')
      if (!result.meta.status == 200)
        return this.$message.error('获取功能列表失败')
      console.log(result)
      this.menuList = result.data
    },
    saveState(activepath) {
      window.sessionStorage.setItem('activepath', activepath)
      this.activePath = activepath
    }
  }
}
</script>
<style lang="less" scope>
.el-container {
  height: 100%;
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
    background-color: #373d41;
    align-items: center;
    line-height: 60px;
    color: #fff;
    font-size: 20px;

    .logo_bt {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
      img {
        height: 60px;
      }
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle-button {
    color: #fff;
    height: 40px;
    line-height: 40px;
    text-align: center;
    font-size: 12px;
    background-color: #4a5064;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
}
.iconfont {
  margin-right: 10px;
}
</style>